# -*- coding: utf-8 -*-
"""
@author: administrator

"""
from datetime import datetime
from pathlib import Path

import torch.nn.modules


def save(net: torch.nn.modules.Module, name: str, epoch: int, path: str = "out"):
    p = Path(path)
    if not p.exists():
        p.mkdir(exist_ok=True)

    now = datetime.now()
    formatted_time = now.strftime("%Y%m%d%H%M%S")
    torch.save(net.state_dict(), "{}/{}_{}_{}.pth".format(path, name, epoch, formatted_time))


def get_device():
    if torch.cuda.is_available():
        return torch.device('cuda')
    elif torch.backends.mps.is_available():
        return torch.device('mps')
    else:
        return "cpu"
